<?
/**
*  结算管理
*  2013/7/14 by zh
*/
include("../webkit/include/simple_db.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];

if($action == "del") {
	$db->doDelete('hncc_money', 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "pay") {
	$sql = "update hncc_money set pay_status=2 where id=".$_GET['id'];
	$db->query($sql);
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['date_start'])) {
		$condition .= " and date_start>='".$_GET['date_start']."'";
	}
	if(isset($_GET['date_end'])) {
		$condition .= " and date_end<='".$_GET['date_end']."'";
	}
	if(isset($_GET['client_name'])) {
		$condition .= " and client_name like '%".$_GET['client_name']."%'";
	}
	if(isset($_GET['pay_status'])) {
		$condition .= " and pay_status = ".$_GET['pay_status'];
	}
	if($_SESSION['user_flag'] == "user") {
		$condition .= " and b.user_name = '".$_SESSION['uid']."'";
	}
	$sql = "select a.id, client_name,pay_status, concat(date_start,'_', date_end) as date_range, order_price, order_count, total_income, calc_money_rate, total_money, calc_period as pay_period, order_date, c.truename as user_name, client_source, calc_time from hncc_money a Left Join hncc_client b On a.client_id=b.id Left Join hncc_user c On b.user_name=c.username where $condition order by a.id desc"; 
	//echo $sql;
	echo to_grid_json($db, $sql, $dic_data, array("client_name"=>"blue_color_mapper","total_money"=>"green_color_mapper", "user_name"=>"red_color_mapper", "date_range"=>"gray_color_mapper"));

} else if($action == "calc") {
	$client_id = $_GET['client_id'];
	$sql = "select * from hncc_client where id=".$client_id; 
	$arr = $db->getOneRecordArray($sql);
	$sql = "select date_end from hncc_money where client_id=".$client_id." order by id desc"; 
	$prev_date_end = $db->getOneValue($sql);
	$calc_result = get_calc_result($arr, $prev_date_end); // 判断是否用上一次的结束日期作为起始点
	$calc_result['client_id'] = $client_id;
	$db->doInsert("hncc_money", $calc_result); // 写入结算记录表
	echo "ok";
}


function get_calc_result(&$arr, $prev_date_end=null) {
	$month = 0;
	switch($arr['pay_period']) {
		case 1:
			$month = 1;
			break;
		case 2:
			$month = 3;
			break;
		case 3:
			$month = 12;
			break;
	}
	$total_income = $arr['order_price'] * $arr['order_count'] * $month; // 周期内总收入
	$money_rate = $arr['money_rate'];  // 代理分成比例
	if($arr['client_source'] == 1) {
		$money_rate = $money_rate - 0.05;  // 结算时按5%减扣
	} else {
		$money_rate = $money_rate - 0.1;    // 结算时按10%减扣
	}
	$total_money = $total_income * $money_rate; // 周期内总分成
	$date_start = get_date_start($arr['order_date'], $prev_date_end);
	$date_end = get_date_end($date_start, $month);
	$result = array(
		'date_start' => $date_start,
		'date_end' => $date_end,
		'total_money' => $total_money,
		'total_income' => $total_income,
		'calc_period' => $arr['pay_period'],
		'calc_money_rate' => $money_rate
	);
	return $result;
}

function get_date_start($order_date, $prev_date_end) {
	if($prev_date_end == null) {
		// 如果是第一条结算单，则以签约时间为起始日期
		$date_start =  date("Y-m-01", strtotime($order_date));
	} else {
		// 如果之前已经生成过结算单，则在结束日期上加1
		$date_start = date("Y-m-d", strtotime("+1 day", strtotime($prev_date_end)));
	}
	return $date_start;
}

function get_date_end($d, $n) {
	return date("Y-m-d", strtotime("-1 day", strtotime("+$n month", strtotime($d))));
}
?>